﻿using AutoMapper;
using Avalon.Core.Data;
using Avalon.Core.Providers;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Avalon.Core.MVC {
    public class BaseGenericEntityController<TViewModel, TModel> : Controller
        where TViewModel : IViewModel, new()
        where TModel : class, IEntity, new() {

        protected override IActionInvoker CreateActionInvoker() {
            IActionProvider provider = ProviderContainer.Current.ActionProvider;
            return provider.GetActionDescriptor().ActionInvoker;
        }



        //IRepository<TModel> _repository = RepositoryConfig.Initialize<TModel>();

        //public ActionResult Index()
        //{
        //    var viewModels = Mapper.Map<IEnumerable<TViewModel>>(_repository.Get());
        //    return View(viewModels);
        //}

        //public ActionResult List()
        //{
        //    var viewModels = Mapper.Map<IEnumerable<TViewModel>>(_repository.Get());
        //    return View(viewModels);
        //}

        //public ActionResult Create()
        //{
        //    var viewModel = new TViewModel();
        //    return View(viewModel);
        //}

        //[HttpPost]
        //public ActionResult Create(TViewModel viewModel)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var model = Mapper.Map(viewModel, _repository.Get(viewModel.Id));
        //        _repository.Add(model);
        //        _repository.SaveChanges();

        //        return RedirectToAction("Detail", new { id = viewModel.Id });
        //    }
        //    return View(viewModel);
        //}

        //public ActionResult CreateReferencedEntity(Guid parentId, string parentName)
        //{
        //    ViewBag.parentId = parentId;
        //    ViewBag.parentName = parentName;

        //    var viewModel = new TViewModel();
        //    return View(viewModel);
        //}

        //[HttpPost]
        //public ActionResult CreateReferencedEntity(TViewModel viewModel, Guid parentId, string parentName)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        Type modelType = ConfigurationManager.Current.GetViewModel(parentName);
        //        IRepository repository = RepositoryConfig.Initialize(modelType);

        //        var parent = repository.Get(parentId);
        //        var child = ModelManipulation.Convert(ConfigurationManager.Current.GetModelTypeByViewModelType(typeof(TViewModel)), viewModel);
        //        ModelManipulation.AddToChildCollection(parent, child);

        //        repository.SaveChanges();

        //        return RedirectToAction("Detail", modelType.Name, new { id = parentId });
        //    }

        //    return View(viewModel);
        //}



        //public ActionResult Edit(Guid id)
        //{
        //    var viewModel = Mapper.Map<TViewModel>(_repository.Get(id));
        //    return View(viewModel);
        //}

        //[HttpPost]
        //public ActionResult Edit(TViewModel viewModel)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var model = Mapper.Map(viewModel, _repository.Get(viewModel.Id));
        //        _repository.Update(model);
        //        _repository.SaveChanges();

        //        return RedirectToAction("Detail", new { id = viewModel.Id });
        //    }
        //    return View(viewModel);
        //}

        //public ActionResult Delete(Guid id, Guid? parentId, string parentName)
        //{
        //    _repository.Delete(id);
        //    _repository.SaveChanges();

        //    if (parentId != null && !string.IsNullOrWhiteSpace(parentName))
        //    {
        //        return RedirectToAction("Detail", GenericControllerFactory.ControllerName(parentName), new { id = (Guid)parentId });
        //    }
        //    return RedirectToAction("List");
        //}

        //public ActionResult Detail(Guid id, Guid? parentId, string parentName)
        //{
        //    var model = _repository.Get(id);
        //    var viewModel = Mapper.Map<TViewModel>(model);
        //    return View(viewModel);
        //}
    }
}
